﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Xiao.Util
{
    public interface IRedisUtil
    {
        string Get(string key);
        void Set(string key, object t, DateTime? expiresSec = null);
        T Get<T>(string key) where T : new();
        Task<string> GetAsync(string key);
        Task SetAsync(string key, object t, DateTime? expiresSec = null);
        Task<T> GetAsync<T>(string key) where T : new();
        bool SetDate(string key, DateTime date);
        Task<bool> SetDateAsync(string key, DateTime date);
        Task<long> DetleteAsync(params string[] key);
        long Detlete(params string[] key);
        void Additional(string key, int conut);
        Task<long> SAdd<T>(string key, params T[] obj);
        Task<long> SRemAsync<T>(string key, params T[] obj);
        Task<T[]> SDiffAsync<T>(params string[] keys);

    }

    public class RedisUtil : IRedisUtil
    {
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string Get(string key)
        {
            return RedisHelper.Get(key);
        }

        public T Get<T>(string key) where T : new()
        {
            return RedisHelper.Get<T>(key);
        }

        /// <summary>
        /// 设置
        /// </summary>
        /// <param name="key"></param>
        /// <param name="t"></param>
        /// <param name="expiresSec"></param>
        public void Set(string key, object t, DateTime? expiresSec = null)
        {
            RedisHelper.Set(key, t);
            if (expiresSec != null) SetDate(key, (DateTime)expiresSec);
        }

        /// <summary>
        /// 异步获取
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task<string> GetAsync(string key)
        {
            return await RedisHelper.GetAsync(key);
        }

        /// <summary>
        /// 获取值泛型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task<T> GetAsync<T>(string key) where T : new()
        {
            return await RedisHelper.GetAsync<T>(key);
        }

        /// <summary>
        /// 修改或新增
        /// </summary>
        /// <param name="key"></param>
        /// <param name="t"></param>
        /// <param name="expiresSec"></param>
        /// <returns></returns>
        public async Task SetAsync(string key, object t, DateTime? expiresSec = null)
        {
            await RedisHelper.SetAsync(key, t);
            if (expiresSec != null) await SetDateAsync(key, (DateTime)expiresSec);
        }

        /// <summary>
        /// 设置超时时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public bool SetDate(string key, DateTime date)
        {
            return RedisHelper.PExpireAt(key, date);
        }

        /// <summary>
        /// 设置超时时间异步
        /// </summary>
        /// <param name="key"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public Task<bool> SetDateAsync(string key, DateTime date)
        {
            return RedisHelper.PExpireAtAsync(key, date);
        }
        /// <summary>
        /// 删除异步
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task<long> DetleteAsync(params string[] key)
        {
            return await RedisHelper.DelAsync(key);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public long Detlete(params string[] key)
        {
            return RedisHelper.Del(key);
        }

        public void Additional(string key, int conut)
        {
            RedisHelper.IncrBy(key, conut);
        }

        /// <summary>
        /// 向集合添加一个或多个成员
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public async Task<long> SAdd<T>(string key, params T[] obj)
        {
            return await RedisHelper.SAddAsync(key, obj);
        }

        /// <summary>
        /// 移除集合中一个或多个成员
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public async Task<long> SRemAsync<T>(string key, params T[] obj)
        {
            return await RedisHelper.SRemAsync(key, obj);
        }
        /// <summary>
        /// 返回给定所有集合的差集
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="keys"></param>
        /// <returns></returns>

        public async Task<T[]> SDiffAsync<T>(params string[] keys)
        {
            return await RedisHelper.SDiffAsync<T>(keys);
        }
    }
}
